﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;

namespace DAO
{
    public class ChuyenVanChuyenDAO : DataProvider
    {
        protected override object GetDataFromDataRow(DataTable dt, int i)
        {


            ChuyenVanChuyenDTO objectDTO = new ChuyenVanChuyenDTO();
            objectDTO.MaChuyen = int.Parse(dt.Rows[i]["MaChuyen"].ToString());
            objectDTO.MaLich = int.Parse(dt.Rows[i]["MaLich"].ToString());
            objectDTO.MaXe = int.Parse(dt.Rows[i]["MaXe"].ToString());
            objectDTO.MaNhanVien = int .Parse(dt.Rows[i]["MaNhanVien"].ToString());
            objectDTO.NgayGioChay = DateTime.Parse(dt.Rows[i]["NgayGioChay"].ToString());
            objectDTO.TinhTrang = int.Parse(dt.Rows[i]["ThoiGianChay"].ToString());
            return (object)objectDTO;
        }

        public ChuyenVanChuyenDTO[] GetAll()
        {
            String strSQL = "Select * from ChuyenVanChuyen ";
            DataTable dt = new DataProvider().ExecuteQuery(strSQL);

            ChuyenVanChuyenDTO[] chuyenvanchuyenDTOs = new ChuyenVanChuyenDTO[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
                chuyenvanchuyenDTOs[i] = (ChuyenVanChuyenDTO)GetDataFromDataRow(dt, i);

            return chuyenvanchuyenDTOs;
        }

        public void insert(ChuyenVanChuyenDTO info)
        {
            string insertCommand = "INSERT ChuyenVanChuyen VALUES(" +
                info.MaChuyen + ", '" +
                info.MaLich + ", '" +
                info.MaXe + "', '" +
                info.MaNhanVien + "', '" +
                info.NgayGioChay + "', '" +
                info.TinhTrang + "')";

            ExecuteNonQuery(insertCommand);
        }

        public void update(ChuyenVanChuyenDTO info)
        {
            string updateCommand = "UPDATE ChuyenVanChuyen " +
                                    "SET MaLich = '" + info.MaLich + "', " +
                                    " MaXe = '" + info.MaXe + "', " +
                                    " MaNhanVien = '" + info.NgayGioChay + "', " +
                                    " NgayGioChay = '" + info.NgayGioChay + "'" +
                                    " TinhTrang = '" + info.TinhTrang + "'" +
                                    " WHERE MaChuyen = " + info.MaChuyen;


            ExecuteNonQuery(updateCommand);
        }

        public void delete(string MaChuyen)
        {
            string deleteCommand = "DELETE FROM ChuyenVanChuyen WHERE MaDoanDuong= " + MaChuyen;
            ExecuteNonQuery(deleteCommand);
        }
    }
}
